package com.sunwayhorizo.health.platform.security.dao.impl;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.Query;

import org.jboss.seam.annotations.Name;

import com.sunwayhorizo.health.common.StrUtil;
import com.sunwayhorizo.health.platform.base.BaseManager;
import com.sunwayhorizo.health.platform.security.dao.SecurityOperationDao;
import com.sunwayhorizo.health.platform.security.persistent.SecurityOperation;

@Stateless
@Name("SecurityOperationDao")
public class SecurityOperationImpl extends BaseManager implements SecurityOperationDao{

	@Override
	public SecurityOperation newSecurityOperation(SecurityOperation operation) {
		super.save(operation);
		return operation;
	}

	@Override
	public void deleteSecurityOperation(List<String> list) {
		super.delete(SecurityOperation.class,list);
	}
	
	@Override
	public SecurityOperation updateSecurityOperation(SecurityOperation operation) {
		super.update(operation);
		return operation;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<SecurityOperation> findSecurityOperationList() {
		return super.entityManager2.createNamedQuery("SecurityOperation.findAll").getResultList();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<SecurityOperation> findSecurityOperationList(String operationName) {
		String jpql = "select so from SecurityOperation so where 1=1 ";
		if( StrUtil.isNotEmpty(operationName)){
			jpql+=" and locate(lower(:operationName),lower(so.operationName) )>0 ";
		}
		Query query = super.entityManager2.createQuery(jpql);
		if( StrUtil.isNotEmpty(operationName)){
			query.setParameter("operationName", operationName);
		}
		return query.getResultList();
	}

	@Override
	public SecurityOperation findSecurityOperationDetailById(String operationId) {
		return super.find(SecurityOperation.class, operationId);
	}
	
	@Override
	public SecurityOperation findSecurityOperationDetailByName(String operationName) {
		return (SecurityOperation)super.entityManager2.createQuery("select so from SecurityOperation so where so.operationName = :operationName").setParameter("operationName", operationName).getSingleResult();
	}
}
